package com.example.woke;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.http.HttpMethods;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.PersistentCookieStore;
import com.loopj.android.http.RequestParams;
import com.woke.method.MyApp;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

public class AdressinfoActivity extends Activity implements OnClickListener {

	private EditText mEdit_youbian,mEdit_adress,mEdit_phone,mEdit_name;
	private String id;
	private TextView mTmoren;
	private View mFrame;
	private int arg2;
	private Intent intent;
	private String is_defalut;
	private String name;
	private String phone;
	private String cityifo;
	private String youbian;
	private MyApp application;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_adressinfo);
		
		application = (MyApp) getApplication();
		intview();
		
	}

	private void intview() {
		intent = getIntent();
		String name = intent.getStringExtra("name");
		String address = intent.getStringExtra("address");
		String phone = intent.getStringExtra("phone");
		String code = intent.getStringExtra("code");
		id = intent.getStringExtra("id");
		arg2 = intent.getIntExtra("arg2", -1);
		is_defalut = intent.getStringExtra("is_defalut");
		mEdit_youbian = (EditText) findViewById(R.id.adressinfo_edit_youbian);
		mEdit_adress = (EditText) findViewById(R.id.adressinfo_edit_adress);
		mEdit_phone = (EditText) findViewById(R.id.adressinfo_edit_phone);
		mEdit_name = (EditText) findViewById(R.id.adressinfo_edit_name);
		mEdit_youbian.setText(code);
		mEdit_adress.setText(address);
		mEdit_phone.setText(phone);
		mEdit_name.setText(name);
		findViewById(R.id.adressinfo_image_back).setOnClickListener(this);
		findViewById(R.id.adressinfo_text_change).setOnClickListener(this);
		findViewById(R.id.adressinfo_text_delete).setOnClickListener(this);
		mFrame = findViewById(R.id.adressinfo_progress_bar);
		mTmoren = (TextView) findViewById(R.id.adressinfo_text_moren);
		mTmoren.setOnClickListener(this);
		if(is_defalut.equals("1")){
			mTmoren.setText("默认地址");
			mTmoren.setClickable(false);
		}
	}
	
	private void getstringdata(){
		name = ""+mEdit_name.getText().toString().trim();
		phone = ""+mEdit_phone.getText().toString().trim();
		cityifo = ""+mEdit_adress.getText().toString().trim();
		youbian = ""+mEdit_youbian.getText().toString().trim();
	}
	

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.adressinfo_image_back:
			finish();
			break;
			
		case R.id.adressinfo_text_moren:
//			if(is_defalut.equals("0")){
//				is_defalut = "1";
//				mTmoren.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_sxzylinered));
//				mTmoren.setTextColor(getResources().getColor(R.color.lightred));
//			}else{
//				is_defalut = "0";
//				mTmoren.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_sxzyline));
//				mTmoren.setTextColor(getResources().getColor(R.color.fmuserline1));
//			}
			
			if(is_defalut.equals("0")){
				is_defalut = "1";
				getstringdata();
				if(name.equals("")){
					Toast.makeText(AdressinfoActivity.this, "请输入名字", Toast.LENGTH_SHORT).show();
				}else if(phone.equals("")){
					Toast.makeText(AdressinfoActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
				}else if(cityifo.equals("")){
					Toast.makeText(AdressinfoActivity.this, "请输入详细地址", Toast.LENGTH_SHORT).show();
				}else{
					sendchangedata(name,phone,cityifo,youbian);
				}
			}
			
			break;
			
		case R.id.adressinfo_text_change:
			getstringdata();
			if(name.equals("")){
				Toast.makeText(AdressinfoActivity.this, "请输入名字", Toast.LENGTH_SHORT).show();
			}else if(phone.equals("")){
				Toast.makeText(AdressinfoActivity.this, "请输入手机号", Toast.LENGTH_SHORT).show();
			}else if(cityifo.equals("")){
				Toast.makeText(AdressinfoActivity.this, "请输入详细地址", Toast.LENGTH_SHORT).show();
			}else{
				sendchangedata(name,phone,cityifo,youbian);
			}
			break;
			
		case R.id.adressinfo_text_delete:
			delechangedata();
			break;

		default:
			break;
		}
	}
	
	private void delechangedata() {
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(AdressinfoActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "delAddress");
		params.put("id", id);
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				try {
					String flag = response.getString("flag");
					if(flag.equals("success")){
						String string = response.getString("info");
						Toast.makeText(AdressinfoActivity.this, ""+string, Toast.LENGTH_SHORT).show();
						intent.putExtra("arg2", arg2);
						setResult(112, intent);
						finish();
					}else{
						Toast.makeText(AdressinfoActivity.this, "删除失败！", Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mFrame.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(AdressinfoActivity.this, "删除失败！", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mFrame.setVisibility(View.GONE);
//				Log.e("response", "���");
			}
		});
	}
	

	private void sendchangedata(String name, String phone, String cityifo, String youbian) {
		AsyncHttpClient ahc = new AsyncHttpClient();
        PersistentCookieStore myCookieStore = new PersistentCookieStore(AdressinfoActivity.this);    
        ahc.setCookieStore(myCookieStore); 
		RequestParams params = new RequestParams();
		params.put("g", "api");
		params.put("m", "user");
		params.put("a", "editAddress_post");
		params.put("id", id);
		params.put("phone", phone);
		params.put("name", name);
		params.put("address", cityifo);
		if(youbian.equals("")){
			params.put("code", "000000");
		}else{
			params.put("code", youbian);
		}
		params.put("is_defalut", is_defalut);
		if (application.getDatas_load () != null) {
		params.put("userid", application.getDatas_load().getId());
		}
		Log.e("55566", params.toString());
		ahc.get(HttpMethods.BASEURL, params, new JsonHttpResponseHandler(){
			@Override
			public void onSuccess(int statusCode, Header[] headers,
					JSONObject response) {
				super.onSuccess(statusCode, headers, response);
				
				Log.e("response", response.toString());
				
				try {
					String flag = response.getString("flag");
					String info = response.getString("info");
					if(flag.equals("success")){
						Toast.makeText(AdressinfoActivity.this, ""+info, Toast.LENGTH_SHORT).show();
						intent.putExtra("arg2", -2);
						setResult(112,intent);
						finish();
					}else{
						Toast.makeText(AdressinfoActivity.this, ""+info, Toast.LENGTH_SHORT).show();
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void onStart() {
				// TODO Auto-generated method stub
				super.onStart();
				mFrame.setVisibility(View.VISIBLE);
			}
			
			@Override
			public void onFailure(int statusCode, Header[] headers,
					String responseString, Throwable throwable) {
				super.onFailure(statusCode, headers, responseString, throwable);
				Toast.makeText(AdressinfoActivity.this, "修改失败！", Toast.LENGTH_SHORT).show();
			}
			
			@Override
			public void onFinish() {
				super.onFinish();
				mFrame.setVisibility(View.GONE);
//				Log.e("response", "���");
			}
		});
	}
}
